package cn.vin.demo;

import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.io.IOException;

public class Demo4 {

    public static void main(String[] args) throws IOException, InterruptedException {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        //设置线程池容量
        scheduler.setPoolSize(1);
        //线程名前缀
        scheduler.setThreadNamePrefix("cal-task-");
        //等待时常
        scheduler.setAwaitTerminationSeconds(3);
        //当调度器shutdown被调用时等待当前被调度的任务完成
//        scheduler.setWaitForTasksToCompleteOnShutdown(true);
        //设置当任务被取消的同时从当前调度器移除的策略
        scheduler.setRemoveOnCancelPolicy(true);
        scheduler.initialize();

        long start = System.currentTimeMillis();
        scheduler.execute(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Thread.currentThread().interrupt();
                }
                System.out.println(Thread.currentThread().getName() + "--------BBBB-----run-"+Thread.currentThread().isInterrupted());
            }
            System.out.println(Thread.currentThread().getName() + "--------BBBB-----run-"+Thread.currentThread().isInterrupted());
        });
        Thread.sleep(3000);
        scheduler.shutdown();
    }

    static class Dog {
        private int type;

        private String name;

        public Dog(int type, String name) {
            this.type = type;
            this.name = name;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

}
